package gw.com.android.ui.trade;

import android.os.Bundle;

import com.gwtsz.android.rxbus.RxBus;
import com.gwtsz.gts2.hx.R;

import gw.com.android.app.AppContances;
import gw.com.android.ui.BaseActivity;
import gw.com.android.ui.trade.Fragment.OrderFailedFragment;
import gw.com.android.ui.trade.Fragment.OrderSuccessFragment;
import gw.com.android.utils.PackStatis;
import www.com.library.app.FragmentsManagerUtil;
import www.com.library.model.DataItemDetail;
import www.com.library.view.BtnClickListener;

/**
 * @author jett
 * @since 2018-04-23.
 */
public class OrderResultActivity extends BaseActivity {

    @Override
    protected int getLayoutView() {
        return R.layout.activity_order_result;
    }

    @Override
    protected void initLayoutView() {
        DataItemDetail item = getIntent().getParcelableExtra("mDetail");
        boolean isSuccess = getIntent().getBooleanExtra("isSuccess", false);
        if (isSuccess) {
            showOrderSucFragment(item);
        } else {
            showOrderFailFragment(item);
        }
    }

    @Override
    protected void initViewData() {
    }

    public void showOrderSucFragment(DataItemDetail mDetail) {
        OrderSuccessFragment mSucFragment = new OrderSuccessFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelable("mDetail", mDetail);
        mSucFragment.setArguments(bundle);
        mSucFragment.setSucClickListener(new BtnClickListener() {

            @Override
            public void onBtnClick(int viewID) {
                if (viewID == R.id.btn_custom_left) {
                    RxBus.getInstance().post("order_open_dialog_listener", "keep");
                    finish();
                    PackStatis.getToService("clickContinue", "Trade", null, null);
                    //MobclickEventUtlis.MobclickEvent(OrderResultActivity.this, "Read_TradeSuc_Tick_Tra");
                }
            }
        });
        FragmentsManagerUtil.instance().addPushMsgTabFragment(this, mSucFragment, mCurrentTag);
        showFragment(mSucFragment);
        if (mDetail.getString("mTradeDir").equals(AppContances.TRADE_TYPE_BUY + "")) {
            PackStatis.getToService(PackStatis.EventAction.BUY.getValue(), PackStatis.EventCategory.TRADE.getValue(), null, "1");
        } else if (mDetail.getString("mTradeDir").equals(AppContances.TRADE_TYPE_SALE + "")) {
            PackStatis.getToService(PackStatis.EventAction.BUY.getValue(), PackStatis.EventCategory.TRADE.getValue(), null, "0");
        }
    }

    public void showOrderFailFragment(final DataItemDetail mDetail) {
        OrderFailedFragment mFailFragment = new OrderFailedFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelable("mDetail", mDetail);
        mFailFragment.setArguments(bundle);
        mFailFragment.setBtnClickListener(new BtnClickListener() {
            @Override
            public void onBtnClick(int viewID) {
                finish();
            }
        });
        FragmentsManagerUtil.instance().addPushMsgTabFragment(this, mFailFragment, mCurrentTag);
        showFragment(mFailFragment);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        RxBus.getInstance().post("order_open_dialog_listener", "close");
    }

}
